﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.BLL;
namespace EnvironmentManagerSystem.HuanTong
{
    public partial class HTAddUserManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                clear();
                dlstReoldBind();
            }
        }

        #region 重置按钮(x)
        //重置事件
        protected void btnClear_Click(object sender, EventArgs e)
        {
            clear();
        }
        //清空数据方法
        public void clear()
        {
            txtName.Text = "";
            txtLogName.Text = "";
            txtPwd.Text = "";
            txtName.Text = "";
            txtTel.Text = "";
            dlstReold.SelectedIndex = 0;
            txtName.Focus();
        }
        #endregion

        #region 确定按钮(x)
        //确定按钮
        protected void btnOk_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                try
                {
                    renData rd = new renData();
                    rd.Username = txtName.Text.Trim();
                    rd.Logname = txtLogName.Text.Trim();
                    rd.Pwd = txtPwd.Text.Trim();
                    rd.Phone = txtTel.Text.Trim();
                    rd.Zhineng = dlstReold.SelectedItem.Text;
                    UserData ud = new UserData();
                    ud.UserName = txtName.Text.Trim();
                    ud.LoginName = txtLogName.Text.Trim();
                    ud.Password = txtPwd.Text.Trim();
                    ud.Phone = txtTel.Text.Trim();
                    ud.RoleId = "环境局";
                    ud.InUse = 2;
                    if (EnvironmentManagerSystem.BLL.User.judgeUser(ud))
                    {
                        Jscript.Alert("已存在相同用户名或登录名！", this);
                        return;
                    }
                    if (EnvironmentManagerSystem.BLL.ren.AddUser(rd))
                    {
                    }
                    if (EnvironmentManagerSystem.BLL.ren.AddUsers(ud))
                    {
                        Response.Write("<script>alert('添加成功');window.parent.art.dialog.list['添加用户'].close();</script>");
                    }
                    else
                    {
                        Jscript.Alert("添加用户失败！", this);
                    }
                }
                catch { }
            }
        }
        //验证文本框
        public Boolean Check()
        {
            if (!txtName.Text.Trim().Equals("")
                && !txtLogName.Text.Trim().Equals("")
                && !txtPwd.Text.Trim().Equals(""))
            {
                if (Regex.IsMatch(txtTel.Text.Trim(), @"^((1\d{10})|(1\d{10}[, ，]))+$"))
                {
                    return true;
                }
                else
                {
                    Jscript.Alert("电话号码输入错误", this);
                    return false;
                }
            }
            else
            {
                Jscript.Alert("用户名,密码,登陆名,类型都不能为空", this);
                return false;
            }
        }
        #endregion

        #region 绑定用户角色(x)
        private void dlstReoldBind()
        {
            dlstReold.DataSource = Role.GetLists();
            dlstReold.DataTextField = "RoleName";
            dlstReold.DataValueField = "ID";
            dlstReold.DataBind();
        }
        #endregion
    }
}
